package com.netty.common.actioncommand;

import com.netty.common.protocol.common.TestPacket;
import io.netty.channel.Channel;
import com.netty.common.protocol.request.MessageRequestPacket;

import java.util.Scanner;

public class SendToUserActionCommand implements ActionCommand {

    @Override
    public void execute(Scanner scanner, Channel channel) {
        System.out.print("聊天对象的用户名：");
        String toUserId = scanner.next();
        System.out.print("给[" + toUserId + "]发送消息：");
        String message = scanner.next();
        channel.writeAndFlush(new MessageRequestPacket(toUserId, message));
    }

    @Override
    public void executeOnConnect(Scanner scanner, Channel channel) {
        System.out.print("聊天对象的用户名：");
        String toUserId = scanner.next();
        System.out.println("--------已与[" + toUserId + "]建立聊天--------");
        while (true) {
            System.out.print("给[" + toUserId + "]发送消息：");
            String message = scanner.next();
            if ("exit".equals(message)) {
                System.out.println("--------已与[" + toUserId + "]退出聊天--------");
                break;
            }
            channel.writeAndFlush(new MessageRequestPacket(toUserId, message));
        }
    }

    public void executeOnTest(Scanner scanner, Channel channel) {
        System.out.print("executeOnTest");
        channel.writeAndFlush(new TestPacket("初始化"));
    }
}
